
#ifndef UI_MDIWINDOW_H
#define UI_MDIWINDOW_H

#include <qmainwindow.h>

#include <qlist.h>

#include "ui_graphicview.h"

#include "ui_layerwidget.h"
#include "ui_recentfiles.h"
#include "ui_pentoolbar.h"
//Added by qt3to4:
#include <QCloseEvent>

#include "pf_document.h"


/**
 * MDI document window. Contains a document and a view (window).
 *

 */
class UI_MDIWindow: public QMainWindow {
    Q_OBJECT

public:
    UI_MDIWindow(PF_Document* doc,
                 QWidget* parent,
                 const char* name=NULL,
                 Qt::WindowFlags wflags=Qt::WDestructiveClose);
    ~UI_MDIWindow();

    void initDoc(PF_Document* doc=NULL);
    void initView();

public slots:

    void slotPenChanged(PF_Pen p);

    void slotFileNew();
    bool slotFileOpen(const QString& fileName, LX_Define::FormatType type);
    bool slotFileSave(bool &cancelled, bool isAutoSave=false);
    bool slotFileSaveAs(bool &cancelled);
    bool slotFileClose(bool force);
    void slotFilePrint();

public:
    /** @return Pointer to graphic view */
    UI_GraphicView* getGraphicView() {
        return graphicView;
    }

    /** @return Pointer to document */
    PF_Document* getDocument() {
        return document;
    }
	
    /** @return Pointer to graphic or NULL */
    PF_Graphic* getGraphic() {
        return document->getGraphic();
    }

	/** @return Pointer to current event handler */
	PF_EventHandler* getEventHandler() {
		if (graphicView!=NULL) {
			return graphicView->getEventHandler();
		}
		else {
			return NULL;
		}
	}

    void addChildWindow(UI_MDIWindow* w);
    void removeChildWindow(UI_MDIWindow* w);
    UI_MDIWindow* getPrintPreview();

    /**
     * Sets the parent window that will be notified if this 
     */
    void setParentWindow(UI_MDIWindow* p) {
        PF_DEBUG->print("setParentWindow");
        parentWindow = p;
    }

    /**
     * @return The MDI window id.
     */
    int getId() {
        return id;
    }

	bool closeMDI(bool force, bool ask=true);

	void setForceClosing(bool on) {
		forceClosing = on;
	}

    friend std::ostream& operator << (std::ostream& os, UI_MDIWindow& w);

signals:
    void signalClosing();

protected:
    void closeEvent(QCloseEvent*);

private:
    /** window ID */
    int id;
    /** ID counter */
    static int idCounter;
    /** Graphic view */
    UI_GraphicView* graphicView;
    /** Document */
    PF_Document* document;
    /** Does the window own the document? */
    bool owner;
    /**
     * List of known child windows that show blocks of the same drawing.
     */
    QList<UI_MDIWindow*> childWindows;
    /**
     * Pointer to parent window which needs to know if this window 
     * is closed or NULL.
     */
    UI_MDIWindow* parentWindow;

	/**
	 * If flag is set, the user will not be asked about closing this file.
	 */
	bool forceClosing;
};


#endif

